package com.ruoyi.duty.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.duty.domain.bo.DutyTcBo;
import com.ruoyi.common.core.domain.vo.DutyTcVo;
import com.ruoyi.duty.service.IDutyTcService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 房屋踏查
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/duty/tc")
public class DutyTcController extends BaseController {

    private final IDutyTcService iDutyTcService;

    /**
     * 查询房屋踏查列表
     */
    @GetMapping("/list")
    public TableDataInfo<DutyTcVo> list(DutyTcBo bo, PageQuery pageQuery) {
        return iDutyTcService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出房屋踏查列表
     */
    @Log(title = "房屋踏查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DutyTcBo bo, HttpServletResponse response) {
        List<DutyTcVo> list = iDutyTcService.queryList(bo);
        ExcelUtil.exportExcel(list, "房屋踏查", DutyTcVo.class, response);
    }

    /**
     * 获取房屋踏查详细信息
     *
     * @param tcId 主键
     */
    @GetMapping("/{tcId}")
    public R<DutyTcVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long tcId) {
        return R.ok(iDutyTcService.queryById(tcId));
    }

    /**
     * 新增房屋踏查
     */
    @SaCheckPermission("duty:tc:add")
    @Log(title = "房屋踏查", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DutyTcBo bo) {
        return toAjax(iDutyTcService.insertByBo(bo));
    }

    /**
     * 修改房屋踏查
     */
    @SaCheckPermission("duty:tc:edit")
    @Log(title = "房屋踏查", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DutyTcBo bo) {
        return toAjax(iDutyTcService.updateByBo(bo));
    }

    /**
     * 删除房屋踏查
     *
     * @param tcIds 主键串
     */
    @SaCheckPermission("duty:tc:remove")
    @Log(title = "房屋踏查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tcIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] tcIds) {
        return toAjax(iDutyTcService.deleteWithValidByIds(Arrays.asList(tcIds), true));
    }
}
